<?php
/**
 * TbMallProductSkusDao.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/16- 2:37 下午
 */

namespace common\dao;


use common\exception\ServiceException;
use common\exception\ValidatorException;
use common\models\PddMallProductSkus;
use Yii;
class PddMallProductSkusDao
{

    /**
     * 写入
     * @param array $data
     * @return PddMallProductSkus
     * @throws ValidatorException
     */
    public function insertData(array $data)
    {
        $mallProductSkus = new PddMallProductSkus();

        $mallProductSkus->load($data,'');

        if (!$mallProductSkus->validate()) {
            throw new ValidatorException($mallProductSkus->errorsToString());
        }

        if($mallProductSkus->save())
        {
            return $mallProductSkus;
        }else{
            throw new ValidatorException($mallProductSkus->errorsToOneString());
        }
    }


    /**
     * 批量删除
     * @param $params
     * @return int
     */
    public function batchDeleteAll($params)
    {
        return PddMallProductSkus::deleteAll($params);
    }


    /**
     * 批量写入
     * @param array $data
     * @return int
     * @throws ServiceException
     * @throws \yii\db\Exception
     */
    public function batchInsert(array $data)
    {
        if(!count($data)) throw new ServiceException('请求参数错误');
        $fields = array_keys($data[0]);
        return Yii::$app->db->createCommand()
            ->batchInsert(PddMallProductSkus::tableName(), $fields, $data)->execute();

    }
}
